import { Icon as CustomIcon } from 'comps/common';
import { Icon } from 'antd';
import UnitOperator from '../containers/unit-operator';
import _ from 'lodash';

const ChildUnits = ({ children, unitDeleteConfirm }) => {
  return (
    <ul className='list child-units'>
      {children.map(unit => {
        const { id, url } = unit;
        return (
          <li key={id} className='child-unit'>
            <a className='unit-delete-link' onClick={() => unitDeleteConfirm.show(unit.id)}><Icon type='minus-circle-o'/> 移除</a>
            <div className='unit-url'>path: {url}</div>
            <UnitOperator unit={unit}/>
          </li>
        );
      })}
    </ul>
  );
};

const RunnableUnit = ({ unit, unitDeleteConfirm }) => {
  const { type, key, url, children } = unit;

  let typeClass;
  let typeText;
  switch (type) {
    case 'LINNER':
      typeClass = 'type-linner';
      typeText = <span><CustomIcon type='icon_box'/>Linner 工程</span>;
      break;
    case 'MAVEN':
    case 'MAVEN_PARENT':
      typeClass = 'type-maven';
      typeText = <span><CustomIcon type='maven'/>Maven 工程</span>;
      break;
    case 'DOCKER_IMAGE':
      typeClass = 'type-docker';
      typeText = <span><CustomIcon type='docker'/>Docker Image</span>;
      break;
    default:
  }

  return (
    <li className='table-list-item runnable-unit'>
      <div className={`table-list-cell runnable-unit-type ${typeClass}`}>
        <a className='runnable-unit-delete' onClick={() => unitDeleteConfirm.show(unit.id)}><Icon type='minus-circle-o'/> 移除</a>
        <div className='runnable-unit-type-text'>{typeText}</div>
        <div className='runnable-unit-name'>{key}</div>
      </div>
      <div className='table-list-cell runnable-unit-content'>
        <div className='unit-url'>
          <CustomIcon type={type === 'DOCKER_IMAGE' ? 'package' : 'git'}/>{url}
        </div>
        <UnitOperator unit={unit}/>
        {_.isEmpty(children) ? undefined : <ChildUnits children={children} unitDeleteConfirm={unitDeleteConfirm}/>}
      </div>
    </li>
  );
};

export default RunnableUnit;
